# _*_ coding: utf-8 _*_
# @Time : 2024-01-20 18:12
# @Author : wf
import base64
import time

from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.binary_location = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
options.add_experimental_option('detach', True)
options.add_experimental_option("excludeSwitches", ["enable-automation"])  # 禁止控制提示

options.add_argument('--headless') # 设置无窗口模式

# --headless 全屏无效
options.add_argument('--start-maximized')  # 设置窗口最大化
options.add_argument('--disable-gpu') # 禁用gpu加速

driver = webdriver.Chrome(options=options)
# driver.get('https://www.jd.com/')
driver.get('https://www.baidu.com')


# 最大化窗口
# --headless 全屏无效
driver.maximize_window()
driver.find_element(By.NAME, 'wd').send_keys('我的世界\n')
time.sleep(2)

# save_screenshot
driver.save_screenshot('我的世界.png')

# get_screenshot_as_file
driver.get_screenshot_as_file('我的世界1.png')

# get_screenshot_as_png
# 字节
data = driver.get_screenshot_as_png()
with open('我的世界2.png', 'wb') as fp:
    fp.write(data)

# get_screenshot_as_base64
base64_str = driver.get_screenshot_as_base64()
# 解码
img_data = base64.b64decode(base64_str)
with open('我的世界3.png', 'wb') as fp:
    fp.write(img_data)

time.sleep(30)
# 退出
driver.quit()
